package com.bdqn.controller;

import com.bdqn.pojo.SysUser;
import com.bdqn.service.SysUserService;
import com.bdqn.service.impl.SysUserServiceImpl;
import com.bdqn.util.Constants;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * IndexController
 *
 * @author LILIBO
 * @since 2024/7/27
 */
@Controller
public class IndexController {

    @Resource
    private SysUserService userService;

    /**
     * 去登录页
     *
     * @return
     */
    @GetMapping("/login")
    public String toLogin() {
        // 加载页面渲染需要的资源
        return "login";
    }

    /**
     * 执行登录操作
     *
     * @return
     */
    @PostMapping("/login")
    public String doLogin(HttpSession session, @RequestParam("account") String account, @RequestParam("password") String password) {
        SysUser sysUser = userService.login(account , password);
        if (sysUser == null) {
            // redirect：重定向（让浏览器发起请求）
            return "redirect:/login";
        }
        session.setAttribute(Constants.USER_SESSION, sysUser);
        return "frame";
    }

    @GetMapping("/user/logout")
    public String toLogout(){
        return "login";
    }


}
